// 确认表导入的时个手动执行 权限转换

/*
    select Id,ProvinceManager__c,DistributionOwner__c,DistributionOwnerManager__c,DirectBusinessOwner__c,DirectBusinessOwnerManager__c,
    TripartiteOwnerManager__c,TripartiteOwner__c,BiochemicalIOwner__c,BiochemicalIOwnerManager__c,ImmunityOwner__c,
    ImmunityOwnerManager__c,ClinicalLaboratoryOwner__c,ClinicalLaboratoryOwnerManager__c,PathologyOwner__c,PathologyOwnerManager__c,
    POCTOwner__c,POCTOwnerManager__c,Account__c,TerminalAccount__c ,SysmexOwner__c,SysmexOwnerManager__c,MicrobeOwner__c,MicrobeOwnerManager__c,
    BDOwner__c,BDOwnerManager__c,MolecularOwner__c,MolecularOwnerManager__c
     from PerformanceRightsConfirmation__c 

*/
global class PerformanceRightsConfirmationBatch implements Database.Batchable<PerformanceRightsConfirmation__c> {
    public String query;

    global PerformanceRightsConfirmationBatch(String query ) {
        this.query = query;
    }

    global Iterable<PerformanceRightsConfirmation__c> start(Database.batchableContext info){
       
       return (List<PerformanceRightsConfirmation__c>)Database.query(query);
    }


    global void execute(Database.BatchableContext BC, list<PerformanceRightsConfirmation__c> scope) {
        Map<Id,Set<Id>> accUserMap = new Map<Id,Set<Id>>();
   
        for (PerformanceRightsConfirmation__c prc : scope ) {
            Set<Id>userset = new Set<Id>();
            //省区管理
            if(prc.ProvinceManager__c != null )userset.add(prc.ProvinceManager__c);
            //分销
            if(prc.DistributionOwner__c != null )userset.add(prc.DistributionOwner__c);
            if(prc.DistributionOwnerManager__c != null )userset.add(prc.DistributionOwnerManager__c);
            //直销
            if(prc.DirectBusinessOwner__c != null )userset.add(prc.DirectBusinessOwner__c);
            if(prc.DirectBusinessOwnerManager__c != null )userset.add(prc.DirectBusinessOwnerManager__c);
            //三方
            if(prc.TripartiteOwnerManager__c != null )userset.add(prc.TripartiteOwnerManager__c);
            if(prc.TripartiteOwner__c != null )userset.add(prc.TripartiteOwner__c);

            //生化
            if(prc.BiochemicalIOwner__c != null )userset.add(prc.BiochemicalIOwner__c);
            if(prc.BiochemicalIOwnerManager__c != null )userset.add(prc.BiochemicalIOwnerManager__c);
            //免疫
            if(prc.ImmunityOwner__c != null )userset.add(prc.ImmunityOwner__c);
            if(prc.ImmunityOwnerManager__c != null )userset.add(prc.ImmunityOwnerManager__c);
            //临检
            if(prc.ClinicalLaboratoryOwner__c != null )userset.add(prc.ClinicalLaboratoryOwner__c);
            if(prc.ClinicalLaboratoryOwnerManager__c != null )userset.add(prc.ClinicalLaboratoryOwnerManager__c);
            //病理
            if(prc.PathologyOwner__c != null )userset.add(prc.PathologyOwner__c);
            if(prc.PathologyOwnerManager__c != null )userset.add(prc.PathologyOwnerManager__c);

            //POCT
            if(prc.POCTOwner__c != null )userset.add(prc.POCTOwner__c);
            if(prc.POCTOwnerManager__c != null )userset.add(prc.POCTOwnerManager__c);

            //希森美康
            if(prc.SysmexOwner__c != null )userset.add(prc.SysmexOwner__c);
            if(prc.SysmexOwnerManager__c != null )userset.add(prc.SysmexOwnerManager__c);
            //微生物
            if(prc.MicrobeOwner__c != null )userset.add(prc.MicrobeOwner__c);
            if(prc.MicrobeOwnerManager__c != null )userset.add(prc.MicrobeOwnerManager__c);

            //bd
            if(prc.BDOwner__c != null )userset.add(prc.BDOwner__c);
            if(prc.BDOwnerManager__c != null )userset.add(prc.BDOwnerManager__c);

            //分子
            if(prc.MolecularOwnerManager__c != null )userset.add(prc.MolecularOwnerManager__c);
            if(prc.MolecularOwner__c != null )userset.add(prc.MolecularOwner__c);


            Set<Id>tmpset1 = accUserMap.containsKey(prc.Account__c) ? accUserMap.get(prc.Account__c) : new Set<Id>();
            tmpset1.addAll(userset);
            if(prc.Account__c != null ) accUserMap.put(prc.Account__c , tmpset1);

            Set<Id>tmpset2 = accUserMap.containsKey(prc.TerminalAccount__c) ? accUserMap.get(prc.TerminalAccount__c) : new Set<Id>();
            tmpset2.addAll(userset);
            if(prc.TerminalAccount__c != null && prc.TerminalAccount__c != prc.Account__c )accUserMap.put(prc.TerminalAccount__c , tmpset2);


        }
        // 删除之前的共享 增加新的共享
        List<AccountTeamMember> delList = new List<AccountTeamMember>();
        List<AccountTeamMember> atList = new List<AccountTeamMember>();

        //审批通过的时候 将销售员 匹配到客户的客户小组
        for (AccountTeamMember atm  : [select Id,UserId ,AccountId,Account.OwnerId ,TeamMemberRole from AccountTeamMember where AccountId in:accUserMap.keySet() ]) {
            //如果是所有人 
            if(accUserMap.containsKey(atm.AccountId) && accUserMap.get(atm.AccountId ).contains(atm.Account.OwnerId )) accUserMap.get(atm.AccountId ).remove(atm.Account.OwnerId);
            //如果是客户小组
            if(accUserMap.containsKey(atm.AccountId ) && accUserMap.get(atm.AccountId ).contains(atm.UserId) ){
                atm.TeamMemberRole = '销售绩效分配客户权限';
            
                atList.add(atm);
                accUserMap.get(atm.AccountId ).remove(atm.UserId);
                continue;
            }
            //如果是之前的
            if(atm.TeamMemberRole == '销售绩效分配客户权限')delList.add(atm);
        }

       
        Set<Id> userset = new Set<Id>();
        for (Id accId :  accUserMap.keySet() ) {
            for (Id userId  : accUserMap.get(accId)) {
                userset.add(userId);
                atList.add(new AccountTeamMember(AccountId=accId,UserId=userId ,TeamMemberRole = '销售绩效分配客户权限' ));
            }
        }

        if(!delList.isEmpty()) delete delList;
        if(!atList.isEmpty()) upsert atList;

        List<AccountShare> ahList = new List<AccountShare>();
        for(AccountShare ah : [select Id , AccountAccessLevel ,CaseAccessLevel from AccountShare where AccountId in:accUserMap.keySet() and  RowCause = 'Team' and UserOrGroupId=:userset ] ){
            ah.AccountAccessLevel = 'Edit';
            ah.CaseAccessLevel = 'Read';

            ahList.add(ah);

        }
        System.debug('PerformanceRightsConfirmationBatch=>'+ahList);
        update ahList;

    }

    global void finish(Database.BatchableContext BC) {

    }
}